﻿using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Web;
using System.Web.UI;
using AnyOffice.BIZ;
using log4net;
namespace AnyOffice.module.oc
{
    public partial class readdisk : Page
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
      
        protected void Page_Load(object sender, EventArgs e)
        {
            string str = (Request.QueryString["attachment_id"] == null) ? "" : Request.QueryString["attachment_id"].ToString();
            string str2 = (Request.QueryString["name"] == null) ? "" : Request.QueryString["name"].ToString();
            if ((str != "") && (str2 != ""))
            {
                try
                {
                    int num;
                    FileInfo info = new FileInfo(Server.MapPath(AttachmentBiz.AttachmentPath) + @"\" + str + @"\" + str2);
                    if (!info.Exists)
                    {
                        return;
                    }
                    string str4 = ".doc.xls.ppt.docx.xlsx.pptx";
                    if (str4.IndexOf(info.Extension.ToLower()) < 0)
                    {
                        return;
                    }
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(info.Name, Encoding.UTF8));
                    string str5 = info.Extension.ToLower();
                    if (str5 != null)
                    {
                        if (!(str5 == ".doc"))
                        {
                            if (str5 == ".xls")
                            {
                                Response.ContentType = "application/vnd.ms-excel";
                            }
                            if (str5 == ".ppt")
                            {
                                Response.ContentType = "application/vnd.ms-powerpoint";
                            }
                            if (str5 == ".docx")
                            {
                                Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                            }
                            if (str5 == ".xlsx")
                            {
                                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            }
                            if (str5 == ".pptx")
                            {
                                Response.ContentType = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                            }
                        }
                        else
                        {
                            Response.ContentType = "application/msword";
                        }
                    }
                    num = 10240;
                    FileStream stream = new FileStream(info.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, num);
                    try
                    {
                        byte[] buffer = new byte[num];
                        int count = 0;
                        int offset = 0;
                        while ((count = stream.Read(buffer, offset, buffer.Length)) > 0)
                        {
                            Response.OutputStream.Write(buffer, offset, count);
                        }
                    }
                    catch (Exception exception)
                    {
                        if (log.IsErrorEnabled)
                        {
                            log.Error("", exception);
                        }
                    }
                    finally
                    {
                        stream.Close();
                    }
                }
                catch (Exception exception2)
                {
                    if (log.IsErrorEnabled)
                    {
                        log.Error("", exception2);
                    }
                }
            }
        } 

    }
}
